<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class Admin extends Backend_Controller {

	/**
	 * 构造函数
	 */
	public function __construct() {
		parent::__construct();
		$this->isValidRequest();
		//加载admin模型
		$this->load->model('backend/Admin_model', 'Admin');
		//加载验证类
		$this->load->library('form_validation');
	}

	/**
	 * 显示后台个人中心下的查看管理员
	 */
	public function index() {
		$result = $this->Admin->getAdminAll($this->page, $this->input->post());
		$this->load->view('backend/admin/index', $result);
	}

	/**
	 * 添加管理员
	 */
	public function add() {
		if ($this->isPost()) {
			$this->form_validation->validateData(array(
				'username',
				'password',
				'passwdConf',
				'role',
				'status'
					), array(
				array('username', 'required|min_length[5]|is_unique[manager.username]'),
				array('password', 'required|min_length[6]'),
				array('passwdConf', 'required|matches[password]'),
				array('role', 'greater_than[0]'),
				array('status', 'integer'),
			));

			if ($this->form_validation->hasErrors()) {
				$this->renderJson(300, $this->form_validation->error);
			}

			$data = array(
				'username' => $this->form_validation->username,
				'password_hash' => md5(md5($this->form_validation->password)),
				'role' => $this->form_validation->role,
				'status' => $this->form_validation->status,
				'created_at' => time()
			);
			if ($this->Admin->addAdmin($data)) {
				$this->renderJson(200, '', array(
					'navTabId' => 'backendadminindex',
					'callbackType' => 'closeCurrent'));
			}
			$this->renderJson(300);
		}

		$this->load->view('backend/admin/add');
	}

	/**
	 * 删除管理员
	 */
	public function delete() {
		$ids = $this->input->post_get('ids');
		if ($this->Admin->deleteAdmin($ids)) {
			$this->renderJson(200);
		}

		$this->renderJson(300);
	}

	/**
	 * 修改管理员信息
	 */
	public function edit() {
		if ($this->isPost()) {
			$this->form_validation->validateData(array(
				'id',
				'role',
				'status'
					), array(
				array('id', 'integer|greater_than[1]'),
				array('role', 'greater_than[0]'),
				array('status', 'integer'),
			));

			if ($this->form_validation->hasErrors()) {
				$this->renderJson(300, $this->form_validation->error);
			}

			$data = array(
				'role' => $this->form_validation->role,
				'status' => $this->form_validation->status,
			);
			if ($this->Admin->updateAdmin($data, $this->form_validation->id)) {
				$this->renderJson(200, '', array(
					'navTabId' => 'backendadminindex',
					'callbackType' => 'closeCurrent'));
			}
			$this->renderJson(300);
		}
		$id = intval($this->get('id'));
		$data = $this->Admin->findAdmin($id);
		$this->load->view('backend/admin/edit', $data);
	}

	/**
	 * 重置密码
	 */
	public function reset_password() {
		if ($this->isPost()) {
			$this->load->library('form_validation');
			$this->form_validation->validateData(array('password', 'newHash', 'passwdConf'), array(
				array('password', 'required|min_length[6]|validate_password[manager.username]'),
				array('newHash', 'required|min_length[6]|differs[password]'),
				array('passwdConf', 'required|matches[newHash]')
			));

			if ($this->form_validation->hasErrors()) {
				$this->renderJson(300, $this->form_validation->error);
			}

			$data = array(
				'password_hash' => md5(md5($this->form_validation->newHash))
			);
			
			$result = $this->Admin->update($data, array('id'=>  $this->identity['id']));
			if($result) {
				$this->renderJson(200, '', array('callbackType' => 'closeCurrent'));				
			}
			$this->renderJson(300);
		}
		$this->load->view('backend/admin/reset_password');
	}

}
